﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace DbGenerator.Model.units.constraint.types
{
    public class GConstraintSet<TTConstraint> : IEnumerable<TTConstraint>, IEnumerator<TTConstraint>
    {
        #region ctors
        public GConstraintSet(string headerFormat)
        {
            _Constraints = new List<TTConstraint>();
            _HeaderFormat = headerFormat;
            _Position = -1;
        }
        #endregion

        #region methods
        public void Add(TTConstraint c)
        {
            _Constraints.Add(c);
        }
        #endregion

        #region properties
        private string _HeaderFormat;
        public string HeaderFormat
        {
            get { return _HeaderFormat; }
            set { _HeaderFormat = value; }
        }

        private List<TTConstraint> _Constraints;
        private int _Position;
        public int Count { get { return _Constraints.Count; } }
        #endregion

        #region overriden methods
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            int constraintCount = _Constraints.Count;
            if (constraintCount == 0)
                return "";
            if (!String.IsNullOrEmpty(_HeaderFormat))
            {
                sb.AppendFormat("\n{0}", _HeaderFormat);
                sb.Append("(");
                for (int i = 0; i < constraintCount; i++)
                {
                    sb.AppendFormat("\n{0}, ", _Constraints[i].ToString());
                }
                sb.Remove(sb.Length - 2, 2);
                sb.Append(") ");
            }
            else
            {
                for (int i = 0; i < constraintCount; i++)
                {
                    sb.AppendFormat("\n{0}", _Constraints[i].ToString());
                }
            }
            return sb.ToString();
        }
        #endregion


        #region IEnumerable<TTConstraint> Members

        public IEnumerator<TTConstraint> GetEnumerator()
        {
            return _Constraints.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        IEnumerator IEnumerable.GetEnumerator()
        {
            return (IEnumerator<TTConstraint>)this;
        }

        #endregion

        #region IEnumerator<TTConstraint> Members

        public TTConstraint Current
        {
            get { return _Constraints[_Position]; }
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            GC.Collect();
        }

        #endregion

        #region IEnumerator Members

        object IEnumerator.Current
        {
            get { return Current; }
        }

        public bool MoveNext()
        {
            return ++_Position < _Constraints.Count;
        }

        public void Reset()
        {
            _Position = -1;
        }

        #endregion
    }
}
